import {Component, EventEmitter, OnInit, Input, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {ActivatedRoute, Router} from '@angular/router';
import {ApmService} from '../apm.service';

@Component({
  selector: 'app-apm-add',
  templateUrl: 'apm-add.component.html',
  styleUrls: ['apm-add.component.css']
})
export class ApmAddComponent implements OnInit {
  region: any[] = [];
  tourisms: any[] = [];
  time: any;

  // ------------------------------------------------------------------------------------------
  @ViewChild('resetFormComponent') resetFormComponent;
  @Output() refreshData = new EventEmitter();
  isVisible = false;
  validateForm: FormGroup;

  showModal(): void {
    this.isVisible = true;
  }

  handleOk(): void {
    const isSuccess: boolean = this.submitForm();
    if (isSuccess) {
      this.resetForm();
      this.isVisible = false;
    }
  }

  handleCancel(): void {
    this.resetForm();
    this.isVisible = false;
  }

  getInvalid(name) {
    return this.resetFormComponent.getInvalidMessage(name);
  }

  submitForm() {
    const data: Element = new Element();
    if (!this.resetFormComponent.validForm()) {
      return false;
    } else {
      for (const i in this.validateForm.controls) {
        if (i) {
          this.validateForm.controls[i].markAsDirty();
          data[i] = this.validateForm.controls[i].value;
        }
      }
      this.create(data);
      return true;
    }
  }

  resetForm(): void {
    this.validateForm.reset();
    for (const key in this.validateForm.controls) {
      if (key) {
        this.validateForm.controls[key].markAsPristine();
      }
    }
  }

  create(data) {
    if (this.time !== undefined) {
      data.beginTime = new Date(this.time[0]);
      data.endTime = new Date(this.time[1]);
  }
    this.mainService.create(data).subscribe(respose => this.refreshData.emit());
  }

  // -------------------------------------------------------------------------------------------------------------------------

  constructor(private fb: FormBuilder,
              private router: Router,
              private activatedRoute: ActivatedRoute,
              private mainService: ApmService) {
    this.validateForm = this.fb.group({
      activeName: [null, [Validators.required, this.nameValidator]],
      activeType: [null, [Validators.required]],
      activeAddress: [null, [Validators.required]],
      telephone: [null, [Validators.required]],
      time: [null, [Validators.required]],
      perUnitPrice: [null, [Validators.required]]
    });
  }
  nameValidator = (control: FormControl): { [s: string]: any } => {
     for (let i = 0; i < this.tourisms.length; i++) {
       if (this.tourisms[i].activeName === control.value && null != control.value) {
         return {projectUrl: {message: '该活动名称已存在!'}};
       }
     }
   }
  ngOnInit() {
    this.mainService.getData().subscribe(response => {
      this.tourisms = response;
    });

  }
}

export class Element {
  id: any;
  activeName: any;
  activeType: any;
  activeAddress: any;
  beginTime: any;
  endTime: any;
  time: any;
  telephone: any;
  perUnitPrice: any;
}

